<?php

namespace App\Http\Controllers\Mgt;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

use App\Repositories\Eloquent\Mgt\ActivityRepository as Activity;

class ActivityController extends Controller
{
    protected $activity;

    public function __construct(Activity $activity)
    {
        parent::__construct();

        $this->activity = $activity;
    }


    public function index()
    {
        $activities = $this->activity->index()->paginate(60);

        $paging = round(count($activities)/2);

        return view($this->view, compact('activities','paging'));
    }

    public function create()
    {
        return view($this->view);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return $this->activity->store($request);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $activity = $this->activity->show($id);

        if ($activity instanceof JsonResponse && $activity->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('activity'));
    }


    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return $this->activity->renew($request, $id);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function normal(Request $request)
    {
        $activities = $this->activity->index(Auth::guard('admin')->user(), 0);

        return view($this->view, compact('activities'));
    }


    public function special(Request $request)
    {
        $activities = $this->activity->index(Auth::guard('admin')->user(), 1);

        return view($this->view, compact('activities'));
    }


    public function show(Request $request, $id)
    {
        $activity = $this->activity->show($id);

        if ($activity instanceof JsonResponse && $activity->getStatusCode() === 404) {
            return abort('404');
        }

        $act = $activity;

        $collections = $activity->datasets($request->action);

        return view($this->view, compact('activity', 'collections', 'act'));
    }


    public function book(Request $request, $id)
    {
        $activity = $this->activity->show($id);

        if ($activity instanceof JsonResponse && $activity->getStatusCode() === 404) {
            return abort('404');
        }

        $act = $activity;

        $collections = $activity->datasets($request->action);

        return view($this->view, compact('activity', 'collections', 'act'));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function status(Request $request, $id)
    {
        $activity = $this->activity->show($id);
        if ($activity instanceof JsonResponse && $activity->getStatusCode() === 404) {
            return abort('404');
        }
        return $this->activity->status($request, $id);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
